
	import {util} from '@/static/js/common.js';


	var routeList = [];//线路列表
	var GroupList = [];//线路列表
	var YuangongList = [];//员工
	var LevelList = []//等级数组
	var astrict = {}
	var nowRoute = ''

	/**
	 * 获取权限的配置
	 */
	const GetAstrict = ()=>{
		return astrict;
	}
	/**
	 * 写入权限的配置
	* @param {string} data :
	 */
	const SetAstrict = (data)=>{
		uni.setStorage({
			key:'ConfigUserInfo',
			data:data
		})
	}
	/**
	 * 获取权限的配置
	 */
	const LoadAstrict = ()=>{
		uni.getStorage({
			key:'ConfigUserInfo',
			success:(res)=>{
				astrict = res.data.astrict_list
			}
		})
	}
	/**
	 * 获取用户信息
	 */
	const GetUserData = ()=>{
		let res = uni.getStorageSync('ConfigUserInfo')

		return res.data
	}
	/**
	 * 线路 读取
	 */

	const loadRoute=(call)=>{
		util.get_request(
			{type:'route_header_list'},
			(res)=>{
				//赋值到左边联动菜单中
				res.list.splice(0,0,{name:'全部线路',id:''})
				
				//赋值到新增客户的下拉框中
				for (var i=0;i<res.list.length;i++) {
					res.list[i].text = res.list[i].name
					res.list[i].value = res.list[i].id
				}
				routeList = res.list
				uni.setStorageSync('routeList',res.list)
				if(call){
					call()
				}
			}
		);
	}
	
	/**
	 * 查看线路
	*/
	const GetRouteItem=(id)=>{
		for (var i=0;i<routeList.length;i++) {
			if(routeList[i].id == id){
				 return routeList[i]
			}
		}
		return {'text':''}
	}

	/**
	 * 写现行线路
	*/
	const SetNowRoute=(e)=>{
		uni.setStorage({
			key: 'route',
			data: e,
			success: function () {
			}
		});
		nowRoute = e
		return  e
	}
	/**
	 * 读现行线路
	*/
	const GetNowRoute=()=>{
		return  nowRoute
	}

	/**
	 * 读 线路
	*/
	const getRoute=()=>{
		if(routeList && routeList.length>0){
			return routeList
		}else{
			routeList = uni.getStorageSync('routeList');
			return  routeList
		}
	}
	/**
	 * 读 仓库的坐标位置
	*/
	const getLatlng=()=>{
		
		var firm = uni.getStorageSync('lbs');
		firm.firm.latitude  = firm.firm.latitude ? parseFloat(firm.firm.latitude):0
		firm.firm.longitude = firm.firm.longitude ? parseFloat(firm.firm.longitude):0
		return  firm.firm
		
	}
	/**
	 * 修改货位号
	 */
	const updatePosition = (goods_id,position,callback)=>{

		if(position.length<=0){
			return
		}
		var content = position.replace(/\s+/g, '')
		util.get_request(
			{
				type:'updatePosition',
				goods_id:goods_id,
				position:content,
			},
			(res)=>{
				
				util.toast(res.msg)
				if(callback){
					callback(content)
				}
			}
		);
	}
	/**
	 * 查看线路
	*/
	const GetRouteName=(id)=>{
		for (var i=0;i<routeList.length;i++) {
			if(routeList[i].id == id){
				 return routeList[i].name
			}
		}
		return ''
	}
	
	/**
	 * 修改授信额度
	 */
	const updateDebt = (user_id,debt,callback)=>{

		if(debt.length<=0){
			return
		}
		var content = debt.replace(/\s+/g, '')
		util.get_request(
			{
				type:'updateDebt',
				user_id:user_id,
				value:content,
				
			},
			(res)=>{
				util.toast(res.msg)
				if(callback){
					callback(content)
				}
			}
		);
	}
	
	/**
	 * 修改库存
	 */
	const updateMargin = (goods_id,margin,callback)=>{


		if(margin.length<=0){
			return
		}
		var content = margin.replace(/\s+/g, '')
		util.get_request(
			{
				type:'updateMargin',
				goods_id:goods_id,
				value:content,
				
			},
			(res)=>{
				util.toast(res.msg)
				if(callback){
					callback(content)
				}
			}
		);
	}
	/**
	 * 修改库存上下限
	 */
	const updateMarginMode = (goods_id,value,mode,callback)=>{

		if(value.length<=0){
			return
		}
		var content = value.replace(/\s+/g, '')
		util.get_request(
			{
				type:'updateMarginMode',
				goods_id:goods_id,
				value:content,
				mode:mode
				
			},
			(res)=>{
				util.toast(res.msg)
				if(callback){
					callback(content)
				}
			}
		);
	}
	/**
	 * 修改商品备注
	 */
	const updateGoodsDescr = (goods_id,value,callback)=>{

		// if(value.length<=0){
		// 	return
		// }
		//var content = value.replace(/\s+/g, '')
		util.get_request(
			{
				type:'updateGoodsDescr',
				goods_id:goods_id,
				value:encodeURIComponent(value)
			},
			(res)=>{
				util.toast(res.msg)
				if(callback){
					callback()
				}
			}
		);
	}
	/**
	 * 设置、取消备注为必填
	*/
	const sendTipmsgType=(list,mode,callback)=>{
		if(list.lenth<=0){
			util.toast("未选中操作项")
			return
		}
		let JsonData = JSON.stringify(list)
		util.get_request(
			{
				type:'sendTipmsgType',
				value:JsonData,
				mode:mode
			},
			(res)=>{
				util.toast(res.msg)
				if(callback){
					callback(res)
				}
			}
		);
	}

	/**
	 * 修改商品赠送积分
	 */
	const updateScore = (goods_id,value,callback)=>{

		util.get_request(
			{
				type:'updateScore',
				goods_id:goods_id,
				value:value,
			},
			(res)=>{
				util.toast(res.msg)
				if(callback){
					callback()
				}
			}
		);
	}
	/**
	 * 清空用户积分记录
	 */
	const UserResetScore = (user_id,callback)=>{
		util.get_request(
			{
				type:'UserResetScore',
				user_id:user_id,
			},
			(res)=>{
				util.toast(res.msg)
				if(callback){
					callback()
				}
			}
		);
	}
	/**
	 * 发送分货位置
	 */
	const goods_pick_number = (order_id,value,callback)=>{
		if(value.length<=0){
			return
		}
		util.get_request(
			{
				type:'goods_pick_number',
				order_id:order_id,
				value:value,
			},
			(res)=>{
				util.toast(res.msg)
				if(callback){
					callback()
				}
			}
		);
	}
	
	/**
	 * 复制指定商品到当前仓库
	 */
	const goods_copy = (goods_id,callback)=>{
	
		util.get_request(
			{
				type:'goods_copy',
				goods_id:goods_id,
			},
			(res)=>{
				util.toast(res.msg)
				if(callback){
					callback(res)
				}
			}
		);
	}
	/**
	 * 获取单张图片信息
	 */
	const GetGoodsImgInfo = (id,callback)=>{
	
		util.get_request(
			{
				type:'GetGoodsImgInfo',
				id:id,
			},
			(res)=>{
				util.showModal("提示",`ID：${id}\n\n上传者：${res.data.user_name}\n\n上传时间：${res.data.up_time}`)
				if(callback){
					callback()
				}
			}
		);
	}
	/**
	 * 修改批发价
	 */
	const updatePifajia = (goods_id,pifajia,callback)=>{
		if(pifajia.length<=0){
			return
		}
		var content = pifajia.replace(/\s+/g, '')
		util.get_request(
			{
				type:'updatePifajia',
				goods_id:goods_id,
				pifajia:content,
				
			},
			(res)=>{
				util.toast(res.msg)
				if(callback){
					callback(content)
				}
			}
		);
	}
	/**
	 * 修改裸价
	 */
	const updateBareMoney = (goods_id,bare_money,callback)=>{
		if(bare_money.length<=0){
			return
		}
		var content = bare_money.replace(/\s+/g, '')
		util.get_request(
			{
				type:'updateBareMoney',
				goods_id:goods_id,
				bare_money:content,
				
			},
			(res)=>{
				util.toast(res.msg)
				if(callback){
					callback(content)
				}
			}
		);
	}
	/**
	 * 修改等级价格
	 */
	const updateLevelMoney = (goods_id,money,mode,callback)=>{
		if(money.length<=0){
			return
		}
		var content = money.replace(/\s+/g, '')
		util.get_request(
			{
				type:'updateLevelMoney',
				goods_id:goods_id,
				money:content,
				mode:mode,
				
			},
			(res)=>{
				util.toast(res.msg)
				if(callback){
					callback(content)
				}
			}
		);
	}
	/**
	 * 修改零售价格
	 */
	const updatePetailPrice = (goods_id,petail_price,callback)=>{
		if(petail_price.length<=0){
			return
		}
		var content = petail_price.replace(/\s+/g, '')
		util.get_request(
			{
				type:'updatePetailPrice',
				goods_id:goods_id,
				petail_price:content,
				
			},
			(res)=>{
				util.toast(res.msg)
				if(callback){
					callback(content)
				}
			}
		);
	}
	
	
	/**
	 * 修改进货价
	 */
	const updateJinhuojia = (goods_id,jinhuojia,callback)=>{
		if(jinhuojia.length<=0){
			return
		}
		var content = jinhuojia.replace(/\s+/g, '')
		util.get_request(
			{
				type:'updateJinhuojia',
				goods_id:goods_id,
				jinhuojia:content,
				
			},
			(res)=>{
				util.toast(res.msg)
				if(callback){
					callback(content)
				}
			}
		);
	}

	/**
	 * 等级 读取
	 */
	const loadLevel=(call)=>{
		util.get_request(
			{type:'level_list'},
			(res)=>{
				LevelList = res.list
				if(call){
					call(res)
				}
			}
		);
	}
	/**
	 * 等级 寻找
	*/
	const GetLevelItem=(id)=>{
		for (var i=0;i<LevelList.length;i++) {
			if(LevelList[i].level_id == id){
				 return LevelList[i]
			}
		}
		return {'text':''}
	}
	/**
	 * 等级 提取全部
	*/
	const getLevel=()=>{
		return LevelList
	}
	/**
	 * 等级 修改
	 * 
	*/
	const Level_update=(name,id,call)=>{
		if(name.length<=0 || id.length<=0){
			util.toast("请先填写欲要修改的名称")
			return ;
		}
		util.get_request(
			{
				type:'Level_update',
				update_id:id,
				name:name,
			},
			(res)=>{
				loadLevel(()=>{
					util.toast(res.msg)
					if(call){
						call()
					}
				}) 
			}
		);
	}

	/**
	 * 读取分类
	 * 
	*/
	const loadHeader=()=>{
		util.get_request(
			{type:'goods_header_list'},
			(res)=>{
				uni.setStorageSync('headerList', res.list);
			}
		);
	}
	/**
	 * 读取分类
	 * 
	*/
	const GetHeader=()=>{
		return uni.getStorageSync('headerList') || [];
	}
	/**
	 * 员工 读取
	 */

	const loadYuangong=(call)=>{
		util.get_request(
			{
				type:'firm_user_list',
				user_type:'yg',
				is_map:1
			},
			(res)=>{
				//赋值到左边联动菜单中
				res.list.splice(0,0,{name:'不限',id:''})
				
				//赋值到新增客户的下拉框中
				for (var i=0;i<res.list.length;i++) {
					res.list[i].text = res.list[i].name
					res.list[i].value = res.list[i].id
				}
				YuangongList = res.list
				uni.setStorageSync('YuangongList',res.list)
				if(call){
					call()
				}
			}
		);
	}
	/**
	 * 查看员工
	*/
	const GetYuangongItem=(id)=>{
		for (var i=0;i<YuangongList.length;i++) {
			if(YuangongList[i].id == id){
				 return YuangongList[i]
			}
		}
		return {}
	}

	

	/**
	 * 读 员工
	*/
	const getYuangong=()=>{
		if(YuangongList && YuangongList.length>0){
			return YuangongList
		}else{
			var YuangongList = uni.getStorageSync('YuangongList');
			return  YuangongList
		}
	}

	/**
	 * 线路-修改专员手机号
	*/
	const routeFirmPhone=(phone,route_id,call)=>{
		
		if(phone.length==0 || route_id.length==0){
			uni.showToast({
				title:"信息输入不完整！",
				icon:'none'
			})
			return ;
		}
		util.get_request(
			{
				type:'firmPhone',
				firmPhone:phone,
				update_id:route_id,
			},
			(res)=>{
				loadRoute(()=>{
					if(call){
						call()
					}
				}) 
			}
		);
	}
	/**
	 * 线路 修改
	 * 
	*/
	const route_update=(name,route_id,call)=>{
		if(name == '' || route_id == ''){
			util.toast("请先填写欲要修改的分类名称")
			return ;
		}
		util.get_request(
			{
				type:'route_update',
				update_id:route_id,
				name:name,
			},
			(res)=>{
				util.toast(res.msg)
				loadRoute(()=>{
					if(call){
						call()
					}
				}) 
			}
		);
	}
	/**
	 * 线路 删除
	 * 
	*/
	const route_delete=(route_id,call)=>{
		if(route_id == ''){
			util.toast("请先选择欲要删除的线路")
			return ;
		}
		uni.showModal({
			content: '是否确定删除？此操作不可逆。删除后有关信息无法恢复！',
			success: function (res) {
				if (res.confirm) {
					util.get_request(
						{
							type:'route_delete',
							id:route_id,
						},
						(res)=>{
							loadRoute(()=>{
								if(call){
									call()
								}
							}) 
						}
					);
				}
			}
		});
	}
	
	/**
	 * 线路-发送增加
	*/
	const route_add=(call)=>{
		
		uni.showModal({
			title:'请输入线路的名称',
			editable:true,
			content:"",
			placeholderText:'一线',
			success: (e)=>{
				if(e.confirm){
					var content = e.content
					if(content.length<=0){
						return
					}
					content = content.replace(/\s+/g, '')
					util.get_request(
						{
							type:'route_add',
							name:content,
						},
						(res)=>{
							util.toast(res.msg)
							loadRoute(()=>{
								if(call){
									call()
								}
							}) 
						}
					);
				}else{
					
				}
			}
		})
	}
	/**
	 * 分组 线路
	*/
	const GetGroupItem=(id)=>{
		for (var i=0;i<GroupList.length;i++) {
			if(GroupList[i].id == id){
				 return GroupList[i]
			}
		}
		return {'text':''}
	}

	const getGroup=()=>{
		return GroupList
	}
	/**
	 * 分组 读取
	 */
	const loadGroup=(call)=>{
		util.get_request(
			{type:'group_header_list'},
			(res)=>{
				//赋值到左边联动菜单中
				res.list.splice(0,0,{name:'不限账号分组',id:'0'})
				
				//赋值到新增客户的下拉框中
				for (var i=0;i<res.list.length;i++) {
					res.list[i].text = res.list[i].name
					res.list[i].value = res.list[i].id
				}
				GroupList = res.list
				if(call){
					call()
				}
			}
		);
	}

	/**
	 * 修改仓库名称
	*/
	const firm_name_update=(name,call)=>{
		uni.showModal({
			title:'请输入新的仓库名称的名称',
			editable:true,
			content:name,
			placeholderText:'请输入新的仓库名称的名称',
			success: (e)=>{
				if(e.confirm){
					var content = e.content
					if(content.length<=0){
						return
					}
					content = content.replace(/\s+/g, '')
					util.get_request(
						{
							type:'firm_name_update',
							name:content
						},
						(res)=>{
							util.toast(res.msg)
							if(call){
								call(content)
							}
						},(res)=>{
							
						}
					);
				}else{
					
				}
			}
		})
	}
	/**
	 * 分组-发送增加、
	*/
	const group_add=(call)=>{
		uni.showModal({
			title:'请输入分组的名称',
			editable:true,
			content:"",
			placeholderText:'零售客户',
			success: (e)=>{
				if(e.confirm){
					var content = e.content
					if(content.length<=0){
						return
					}
					content = content.replace(/\s+/g, '')
					util.get_request(
						{type:'groupAdd',name:content},
						(res)=>{
							util.toast(res.msg)
							loadGroup(()=>{
								if(call){
									call()
								}
							}) 
							
						}
					);
				}else{
					
				}
			}
		})
	}
	/**
	 * 修改密码
	*/
	const firm_user_password=(uid,updateDate=false)=>{
		uni.showModal({
			title:'请输入新密码',
			editable:true,
			content:"",
			placeholderText:'',
			success: (e)=>{
				if(e.confirm){
					var content = e.content
					if(content.length<=0){
						return
					}
					content = content.replace(/\s+/g, '')
					util.get_request(
						{
							type:'firm_user_password',
							uid:uid,
							password:content
						},
						(res)=>{
							util.toast(res.msg)
							if(updateDate == true){
								util.goto("/pages/login/login")
							}
						}
					);
				}else{
					
				}
			}
		})
	}
	/**
	 * 分组 删除
	 * 
	*/
	const groupDelete=(id,call)=>{
		if(id.length <=0){
			util.toast("请先选择欲要删除的")
			return ;
		}
		uni.showModal({
			content: '是否确定删除？此操作不可逆。删除后有关信息无法恢复！',
			success: function (res) {
				if (res.confirm) {
					util.get_request(
						{type:'groupDelete',id:id},
						(res)=>{
							loadGroup(()=>{
								if(call){
									call()
								}
							}) 
						}
					);
				}
			}
		});
	}
	
	/**
	 * 分组 修改
	 * 
	*/
	const group_update=(name,id,call)=>{
	
		if(name.length<=0 || id.length<=0){
			util.toast("请先填写欲要修改的名称")
			return ;
		}
		util.get_request(
			{
				type:'group_update',
				update_id:id,
				name:name,
			},
			(res)=>{
				loadGroup(()=>{
					util.toast(res.msg)
					if(call){
						call()
					}
				}) 
			}
		);
	}
	

	export const routeData= { 
		loadRoute,getRoute,GetRouteItem,
		loadGroup,getGroup,GetGroupItem,
		updatePosition,
		LoadAstrict,GetAstrict,SetAstrict,
		getLevel,updateMargin,updatePifajia,updateJinhuojia,GetRouteName,
		getYuangong,GetYuangongItem,loadYuangong,
		routeFirmPhone,
		route_update,
		route_delete,
		route_add,
		group_add,
		groupDelete,
		group_update,
		updateBareMoney,
		updateLevelMoney,
		updatePetailPrice,
		updateMarginMode,
		loadLevel,GetLevelItem,getLevel,Level_update,updateGoodsDescr,GetNowRoute,SetNowRoute,
		updateScore,GetGoodsImgInfo,
		goods_pick_number,
		goods_copy,UserResetScore,updateDebt,sendTipmsgType,firm_user_password,firm_name_update,getLatlng,
		GetHeader,loadHeader,GetUserData
	}